package com.partners.model;

import com.partners.configure.wechatmp.WxMpConfiguration;
import me.chanjar.weixin.common.error.WxErrorException;
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.bean.template.WxMpTemplateData;
import me.chanjar.weixin.mp.bean.template.WxMpTemplateMessage;
import org.springframework.stereotype.Component;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @author liangyongle
 * @date 2018-12-20
 */
@Component
public class TemplateMsg {
    public final static String appId = "wx1b38cf3729aaa2a7";

    private final String  liuId = "238b82b6f1ca-14a9-8e11-e10a-e85bba8a";

    private final String  boseId = "238b82b6f1ca-14a9-8e11-f10a-83033af4";


    public void sendTemplateMsg(String faceId ,Byte type){
        String first = type == 1?"欢迎老客户再次莅临蔽店":"欢迎新客户光临蔽店";
        String openId = "oz3Bbs296WBwx91qQGVVO6Xy9wGU";
        if(faceId == liuId){
            first = type ==1?"欢迎刘总再次莅临蔽店":"欢迎刘总光临蔽店";
            openId = "oz3Bbs6E53NCu1H02DZDmvfBgEvY";
        }else if(faceId == boseId){
            first = type ==1?"欢迎老板再次莅临蔽店":"欢迎老板光临蔽店";
            openId = "oz3Bbs6MdqPTuHktivZ0IYOnv2sU";
        }
        sendMsg(first,openId);
    }

    private void sendMsg(String first, String openId) {
        final WxMpService wxService = WxMpConfiguration.getMpServices().get(appId);
        String templateId = "NK8LnJz8oAnQvA4ZfSdluEgTUwEyRLLCDJl3XzQcjNe";
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy年MM月dd日");
        WxMpTemplateMessage templateMessage = WxMpTemplateMessage.builder().toUser("oR3V81eAmZU4A_mDiuL6uytjCGpM")
            .templateId(templateId).url("").build();
        templateMessage.addData(new WxMpTemplateData("first",first,"#ff510"));
        templateMessage.addData(new WxMpTemplateData("keyword1","赶紧去联系招呼","#ff510"));
        templateMessage.addData(new WxMpTemplateData("keyword2",dateFormat.format(new Date()),"#ff510"));
        templateMessage.addData(new WxMpTemplateData("remark","消息来自智慧门店","#ff510"));
        try{
            wxService.getTemplateMsgService().sendTemplateMsg(templateMessage);
            //wxMpService.getTemplateMsgService().sendTemplateMsg(templateMessage);
        }catch(WxErrorException e){
            e.printStackTrace();
        }


    }


}
